import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class FormBuilder {
  static Widget buildXbFormTextArea(BuildContext context,
      {String? labelText,
      String? hintText,
      String? tipsText,
      FocusNode? focusNode,
      bool obscureText = false,
      bool isRequired = false,
      bool isMarginBottom = true,
      bool isSelection = false,//是否启用文本选择和交互式操作
      Color fillColor = Colors.white,
      TextEditingController? controller,
      Function? onChange,
      Function? onSaved,
      Function? onTap,
      Function? validator,
      int? maxLength,
      Color? titleColor,
      Color? hintTextColor}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (labelText != null && labelText.isNotEmpty)
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              if (isRequired)
                Padding(
                  padding: EdgeInsets.only(right: 5.rpx),
                  child: Text('*',
                      style: TextStyle(
                        fontSize: 30.rpx,
                        color: Colors.redAccent,
                        fontWeight: FontWeight.w500,
                      )),
                ),
              Text(
                labelText,
                textAlign: TextAlign.left,
                style: TextStyle(
                  fontSize: 28.rpx,
                  height: 1.3,
                  color: titleColor??Colors.black.withOpacity(0.45),
                  fontWeight: FontWeight.w500,
                ),
              ),
              if (tipsText != null && tipsText.isNotEmpty)
                GestureDetector(
                    onTap: () {
                      EasyLoading.showToast(tipsText);
                    },
                    child: Padding(
                      padding: EdgeInsets.only(
                        left: 8.rpx,
                      ),
                      child: Icon(Icons.help_outline_rounded,
                          size: 32.rpx,
                          color: Theme.of(context).colorScheme.primary),
                    )),
            ],
          ),
        if (labelText != null && labelText.isNotEmpty)
          SizedBox(
            height: 16.rpx,
          ),
        TextFormField(
          focusNode: focusNode,
          autofocus: false,
          enableInteractiveSelection: isSelection,
          controller: controller,
          maxLines: 4,
          keyboardType: TextInputType.multiline,
          textInputAction: TextInputAction.done,
          style: TextStyle(
            fontSize: 28.rpx,
            color: Colors.black.withOpacity(0.85),
          ),
          decoration: InputDecoration(
              hintText: hintText ?? "",
              fillColor: fillColor,
              filled: true,
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.black12,
                  width: 2.rpx,
                  style: BorderStyle.solid,
                ),
                borderRadius: BorderRadius.circular(14.rpx),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Theme.of(context).colorScheme.primary,
                  width: 2.rpx,
                  style: BorderStyle.solid,
                ),
                borderRadius: BorderRadius.circular(14.rpx),
              ),
              focusedErrorBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.redAccent,
                  width: 2.rpx,
                  style: BorderStyle.solid,
                ),
                borderRadius: BorderRadius.circular(14.rpx),
              ),
              errorBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.redAccent,
                  width: 2.rpx,
                  style: BorderStyle.solid,
                ),
                borderRadius: BorderRadius.circular(14.rpx),
              ),
              hintStyle: TextStyle(
                color: hintTextColor??Colors.black.withOpacity(0.45),
                fontSize: 28.rpx,
              ),
              errorStyle: const TextStyle(
                color: Colors.redAccent,
              ),
              contentPadding:
                  EdgeInsets.symmetric(vertical: 24.rpx, horizontal: 30.rpx)),
          obscureText: obscureText,
          onSaved: onSaved as void Function(String?)?,
          onTap: onTap as void Function()?,
          validator: validator as String? Function(String?)?,
          inputFormatters: [LengthLimitingTextInputFormatter(maxLength)],
          onChanged: onChange as void Function(String)?,

        ),
        if (isMarginBottom)
          SizedBox(
            height: 35.rpx,
          ),
      ],
    );
  }
}
